<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" href="css/login.css" />
    <script src="js/jquery.js"></script>
  </head>
  <body>
    <div class="header">
      <img src="img/loginLogo.png" alt="" />
      <p>欢迎登录</p>
    </div>
    <div class="center">
      <div class="logo"><img src="img/loginBannerImg.png" alt="" /></div>
      <div class="box">
        <span class="dl">账户登录</span>
        <div class="box-center">
          <div>
            <span class="iconfont">&#xe659;</span
            ><input class="txt" placeholder="请输入手机号" type="text" />
          </div>
          <div>
            <span class="iconfont">&#xe64b;</span
            ><input class="pass" placeholder="请输入密码" type="password" />
          </div>

          <button class="btn">登录</button>
          <p>还没有账户？去<a href="Register.html">免费注册</a></p>
        </div>
      </div>
    </div>
    <script src="js/cookie.js"></script>
    <script>
      let zh = getCookie("zh");
      let mm = getCookie("mm");
      if (zh) {
        $(".txt").val(zh);
        $(".pass").val(mm);
      }
      $(".txt").blur(function () {
        let a = /^1(3|4|5|6|7|8|9)\d{9}$/;
        let b = this.value;
        if (a.test(b) == false) {
          alert("请输入正确的手机号");
        }
      });
      $(".pass").blur(function () {
        let a = /^[a-zA-Z_]\w{5,14}$/;
        let b = this.value;
        if (a.test(b) == false) {
          alert("请输入正确的手机号");
        }
      });
      //登录
      $(".btn").click(function () {
        if ($(".txt").val() == "" || $(".pass").val() == "") {
          alert("手机号或密码不能为空");
          return;
        }
        $.get("http://jx.xuzhixiang.top/ap/api/login.php", {
          username: $(".txt").val(),
          password: $(".pass").val(),
        }).then((res) => {
          console.log(res);
          if (res.code == 0) {
            alert("登录失败," + res.msg);
          } else {
            alert("登录成功，即将跳转页面");
            setTimeout(function name(params) {
              removeCookie("zh");
              removeCookie("mm");
              location.href = "index.html";
            }, 1000);
          }
          let id = res.data.id;
          let tx = $(".txt").val();
          document.cookie = "uid=" + id;
          document.cookie = "tx=" + tx;
        });
      });
    </script>
  </body>
</html>
